package eu.fishcode.android.utils;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.Timer;

public class FileChangeListner implements ActionListener {

	private long timeStamp;
	private File file;
	private final Timer timer;
	private ActionListener actionListener;
	
	public FileChangeListner(File file, ActionListener ai) {
		this.timer = new Timer(1500, this);
		this.timeStamp = file.lastModified();
		this.file = file;
		actionListener = ai;
		timer.start();
	}

	public void setFile(File file) {
		timer.stop();
		this.timeStamp = file.lastModified();
		this.file = file;
		timer.start();
	}
	
	private boolean isFileUpdated() {

	  if( this.timeStamp != file.lastModified() ) {
	    this.timeStamp = file.lastModified();
	    return true;
	  }
	  return false;
	}

	public void actionPerformed(ActionEvent arg0) {
		if(isFileUpdated()) {
			actionListener.actionPerformed(new ActionEvent(this, 1, "updated"));
		}
	}
	
	
}
